import akka.actor.{Actor, ActorRef, ActorSystem, Props}

import scala.beans.BeanProperty
import scala.io.StdIn
import scala.util.Random

class PlayerActor extends Actor {

  val choices = Array("剪刀", "石头", "布")
  var self_win, self_lose, self_equal = 0
  var other_name:String = "对方"

//  def this(name:String){
//    this.other_name = name
//  }

  def initEnemy(name: String): Unit ={
    this.other_name = name
  }

  def fight(choose: Int) ={
    println(s"你出了：${choices(choose - 1)}")
    println(s"${other_name}出拳！")
//    val random = new Random(3)
    val oth = Random.nextInt(2);
    println("==> " + oth)
    println(s"${other_name}出了：${choices(oth)}")
    val rst = isSelfWin(choose, oth);
    //统计局数
    if(rst == -1) self_lose = self_lose + 1
    else if(rst == 0) self_equal = self_equal + 1
    else self_win = self_win + 1

    val str = if(rst == -1) s"糟糕！${other_name}赢了"
    else if(rst == 0) "和局！下次继续努力！"
    else "恭喜，你赢了！"
    println(s"结果${str}")
    println(s"是否开始下一轮游戏（y/n）")
  }

  def calculateScore(b_self: Boolean): Int = {
    val score: Int = if(b_self)
      self_win * 2 + self_equal * 1
    else {
      self_lose * 2 + self_equal * 1
    }
    score
  }

  def isSelfWin(my: Int, other: Int): Int = {
  val result: Int = if(my == other) 0
    else if(my == 1 && other == 3 || my == 2 && other == 1 || my == 3 && other == 2 ) 1
    else -1;
    result
  }
  def printScore(): Unit ={
    println("-------------------------------------------")
    println(s"${other_name} VS 游客（自己）")
    println(s"对战次数${self_win + self_lose + self_equal}次")
    println("姓名   得分    胜局    和局    负局")
    println(s"自己\t\t${calculateScore(true)}\t\t${self_win}\t\t${self_equal}\t\t${self_lose}")
    println(s"${other_name}\t\t${calculateScore(false)}\t\t${self_win}\t\t${self_equal}\t\t${self_lose}")
    println("-------------------------------------------")
  }

  //接收消息并进行处理
  override def receive: Receive = {
    case "1" => fight(1)
    case "2" => fight(2)
    case "3" => fight(3)
    case "y" => println("开始下一轮游戏")
    case "n" => {
      println("退出游戏！")
      printScore()
      //关闭自己
      context.stop(self)
      //关闭ActorSystem
      context.system.terminate()
    }
    case "*" => other_name = "*"
  }
}

object PlayerActor {

  //通过ActorSystem创建线程池对象myFactory
  private val myFactory = ActorSystem("myFactory")
  //通过myFactory.actorOf来创建一个Actor
  private val playerActorRef: ActorRef = myFactory.actorOf(Props[PlayerActor], "playerActor")

  def main(args: Array[String]): Unit = {

//    val random = new Random(3)
//    val lst = List.fill(100)(random.nextInt(3))
//    println(lst)

    println("猜拳开始")
    val roles = Array("刘备","关羽","张飞")

    var in_id: Int = -1;
    var in_ok: Boolean = false;

    var inited: Boolean = false;
    while (!inited){
      if(in_id == -1){
        println("请选择对战角色：（1.刘备  2.关于  3.张飞）")
      } else if (!in_ok){
        println(s"要开始了吗？ y/n")
      }

      val input: String = StdIn.readLine()
      if(in_id == -1){
        if(input != "1" && input != "2" && input != "3"){
          println("输入不规范，请重新选角！")
        } else {
          in_id = input.toInt - 1;
          val other: String = roles(in_id);
          println(s"你选择了与${other}对战")
//          helloActorRef.initEnemy(other)
          playerActorRef ! other
        }
      } else if(!in_ok) {
        if(input != "y" && input != "n"){
          println("输入不规范，请重新准备！")
        } else {
          in_ok = if(input == "y") {
            inited = true;
            true
          } else {
            false
          };
        }
      }
    }

    println("开始比赛！")

    var flag = true
    while (flag) {
      println("请输入出拳：1.剪刀 2.石头 3.布")
      var choose: String = StdIn.readLine()
      if(choose != "1" && choose != "2" && choose != "3"){
        println("输入不规范，默认出布！")
        choose = "3"
      }
      //!发送消息
      playerActorRef ! choose

      var is_continue: String = StdIn.readLine()
      if(is_continue != "y" && is_continue != "n"){
        println("输入不规范，默认继续！")
        is_continue = "y"
      }
      playerActorRef ! is_continue
      if (is_continue.equals("n")) {
        flag = false
      }

      //让程序休眠100毫秒
      Thread.sleep(100)
    }

  }

}
